function calculate() {
  // 获取输入值
  const pv = parseFloat(document.getElementById('pv').value);
  const ac = parseFloat(document.getElementById('ac').value);
  const ev = parseFloat(document.getElementById('ev').value);

  // 计算
  const cv = ev - ac;
  const sv = ev - pv;
  const cpi = ev / ac;
  const spi = ev / pv;

  // 更新结果
  document.getElementById('cv').textContent = cv.toFixed(2);
  document.getElementById('sv').textContent = sv.toFixed(2);
  document.getElementById('cpi').textContent = cpi.toFixed(2);
  document.getElementById('spi').textContent = spi.toFixed(2);

  // 成本偏差分析
  let analysis = '';
  if (cv < 0) {
      analysis = '成本偏差为负，说明实际成本超出了计划成本。';
  } else if (cv > 0) {
      analysis = '成本偏差为正，说明实际成本低于计划成本。';
  } else {
      analysis = '成本偏差为零，说明实际成本与计划成本一致。';
  }

  document.getElementById('analysis').textContent = analysis;

  // 绘制图表
  const ctx = document.getElementById('costChart').getContext('2d');
  const costChart = new Chart(ctx, {
      type: 'bar',
      data: {
          labels: ['计划值 (PV)', '实际成本 (AC)', '挣值 (EV)'],
          datasets: [{
              label: '成本数据',
              data: [pv, ac, ev],
              backgroundColor: [
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(153, 102, 255, 0.2)'
              ],
              borderColor: [
                  'rgba(75, 192, 192, 1)',
                  'rgba(255, 99, 132, 1)',
                  'rgba(153, 102, 255, 1)'
              ],
              borderWidth: 1
          }]
      },
      options: {
          scales: {
              y: {
                  beginAtZero: true
              }
          }
      }
  });
}
